;Copyright 2007-2010 Niko Carpenter and Tyler Spivey
;    This file is part of Linna Home Server.
;    Linna Home is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;    any later version.

;    Linna Home is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.

;    You should have received a copy of the GNU General Public License
;    along with Linna Home.  If not, see http://www.gnu.org/licenses/.

#include-once
Func p_fread($id,$arg)
$fread_tmpdata = StringSplit($cons[$id][$C_pdata],@lf)
if $arg = @lf Then $arg = "+"
$argsp = StringSplit($arg, "")
for $i = 1 to $argsp[0]
if StringRegExp($argsp[$i], "^[a-zA-Z/?=]$", 0) Then ExitLoop
Next
Global $args[3]
$args[0] = StringLeft($arg, $i-1)
$arg = StringTrimLeft($arg, StringLen($args[0]))
$argsp = StringSplit($args[0], ",")
if $argsp[0] = 2 Then
if $argsp[1] = "$" Then $argsp[1] = $FRead_TMPData[0]
if $argsp[1] = "." Then $argsp[1] = $cons[$id][$C_pdata2]
if StringInStr($argsp[1], "+") or StringInStr($argsp[1], "-") Then $argsp[1] = FRead_ParseLNum($ID,$Argsp[1])
if StringIsInt($argsp[1]) = 0 or StringIsInt($argsp[2]) = 0 or $argsp[1] < 1 or $argsp[1] > $fread_tmpdata[0] or $argsp[2] < 1 or $argsp[2] = "$" Then $argsp[2] = $FRead_TMPData[0]
if $argsp[2] = "." Then $argsp[2] = $cons[$id][$C_pdata2]
if StringInStr($argsp[2], "+") or StringInStr($argsp[2], "-") Then $argsp[2] = FRead_ParseLNum($ID,$Argsp[2])
if $argsp[2] > $fread_tmpdata[0] or $argsp[1] > $argsp[2] Then
dSend($id,"?")
Else
for $i = $argsp[1] to $argsp[2]
p_fread($Id,$i&$arg)
Next
EndIf
return
ElseIf $argsp[0] = 1 Then
if $args[0] = "$" Then $args[0] = $FRead_TMPData[0]
if $args[0] = "." Then $args[0] = $cons[$id][$C_pdata2]
if StringInStr($args[0], "+") or StringInStr($args[0], "-") Then $args[0] = FRead_ParseLNum($ID,$Args[0])
Else
dSend($id,"?")
return
EndIf
$args[1] = StringLeft($arg, 1)
$args[2] = StringTrimLeft($arg, 1)
if $cons[$id][$C_pdata2] = "" Then $cons[$id][$C_pdata2] = $fread_tmpdata[0]
if $args[1] = "=" Then $args[1] = "eq"
if $args[1] = "/" Then $args[1] = "slash"
if $args[1] = "?" Then $args[1] = "question"
If $args[0] <> "" Then
If $args[0] > $fread_tmpdata[0] or $args[0] < 1 Then
dSend($id,"?")
return
Else
$cons[$id][$C_pdata2] = $args[0]
EndIf
EndIf
if $args[1] <> "" Then
call("fread_"&$args[1],$id,$args[2],$args[0],$fread_tmpdata)
if @Error Then dSend($id,"?")
Else
fread_p($id,"",$args[0],$fread_tmpdata)
EndIf
EndFunc
func fread_q($id,$str,$line,$fread_tmpdata)
lreturn($id)
EndFunc
func fread_p($id,$str,$line,$fread_tmpdata)
dSend($id,$fread_tmpdata[$cons[$id][$C_pdata2]])
EndFunc
func fread_eq($id,$str,$line,$fread_tmpdata)
if $line = "" Then $line = $fread_tmpdata[0]
dSend($id,$line)
EndFunc
Func fread_slash($id,$str,$line,$fread_tmpdata)
if $str = "" Then $str = $cons[$id][$C_pdata4]
$cons[$id][$C_pdata4] = $str
if $cons[$id][$C_pdata2] = $fread_tmpdata[0] Then
dSend($id,"?")
return
EndIf
$f = 0
for $i = $cons[$id][$C_pdata2]+1 to $fread_tmpdata[0]
if StringRegExp($fread_tmpdata[$i],$str) Then
$cons[$id][$C_pdata2] = $i
$f = 1
ExitLoop
EndIf
Next
if $f = 0 Then
dSend($id,"?")
Else
fread_p($id,"",$line,$fread_tmpdata)
EndIf
EndFunc
Func fread_question($id,$str,$line,$fread_tmpdata)
if $str = "" Then $str = $cons[$id][$C_pdata4]
$cons[$id][$C_pdata4] = $str
if $cons[$id][$C_pdata2] = $fread_tmpdata[0] Then
dSend($id,"?")
return
EndIf
$f = 0
$i = $cons[$id][$C_pdata2]-1
While $i >= 1
if StringRegExp($fread_tmpdata[$i],$str) Then
$cons[$id][$C_pdata2] = $i
$f = 1
ExitLoop
EndIf
$i -= 1
Wend
if $f = 0 Then
dSend($id,"?")
Else
fread_p($id,"",$line,$fread_tmpdata)
EndIf
EndFunc
Func fread_l($id,$str,$line,$fread_tmpdata)
dSend($id,"^"&$fread_tmpdata[$cons[$id][$C_pdata2]]&"$")
EndFunc
func fread_z($id,$str,$line,$fread_tmpdata)
if $str = "" Then $str = $cons[$id][$C_pdata5]
if not StringIsInt($str) or $cons[$id][$C_pdata2] = $fread_tmpdata[0] Then
dSend($id,"?")
return
EndIf
$cons[$id][$C_pdata5] = $str
if $str+$cons[$id][$C_pdata2]+1 > $fread_tmpdata[0] Then $str = $fread_tmpdata[0]-$cons[$id][$C_pdata2]
$start = $cons[$id][$C_pdata2]+1
for $i = $start to $start+$str-1
$cons[$id][$C_pdata2] = $i
fread_p($id,"",$line,$fread_tmpdata)
next
EndFunc
func fread_f($id,$str,$line,$fread_tmpdata)
dSend($id,$cons[$id][$C_pdata3])
EndFunc
Func Fread_ParseLNum($Id,$LNum)
if StringIsInt(StringReplace(StringReplace($LNum, "-", ""), "+", "")) = 0 and StringReplace(StringReplace($LNum, "-", ""), "+", "") <> "" Then return ""
if StringIsInt(StringLeft($LNum, 1)) Then $LNum = "+"&$LNum
$total = $cons[$id][$C_pdata2]
While $LNum <> ""
$s = StringSplit($LNum, "")
for $i = 2 to $s[0]
if not StringIsInt($s[$i]) Then ExitLoop
Next
$wstr = StringLeft($LNum, $i-1)
$LNum = StringTrimLeft($LNum, StringLen($wstr))
$op = StringLeft($wstr, 1)
$wstr = StringTrimLeft($wstr, 1)
if not StringIsInt($wstr) Then $wstr = 1
if $op = "+" Then
$total += $wstr
Else
$total -= $wstr
EndIf
Wend
return $total
EndFunc